package net.vanchu.mortgagecalculator.mortgage.calculate.single;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.common.commonView.call.PopupWindowHelper;
import net.vanchu.mortgagecalculator.common.commonView.component.SelectingOptionsLayout;
import net.vanchu.mortgagecalculator.common.commonView.component.WheelView;
import net.vanchu.mortgagecalculator.common.commonView.component.WritingOptionLayout;
import net.vanchu.mortgagecalculator.common.config.LoanRate;
import net.vanchu.mortgagecalculator.common.config.LoanWay;
import net.vanchu.mortgagecalculator.common.config.OptionsProvider;
import net.vanchu.mortgagecalculator.mortgage.calculate.CalculateResultListener;
import net.vanchu.mortgagecalculator.mortgage.calculate.OnLoanWaySwitchListener;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Granton on 2016/12/19.
 */
public class CalSingleLoansFragment extends Fragment implements CalSingleLoanContract.View {

    public static final String WAY = "WAY";

    private static final String TAG = CalSingleLoansFragment.class.getSimpleName();

    private View parentView;

    private CalSingleLoanContract.Presenter presenter;

    private SelectingOptionsLayout layoutLoansWay;

    private SelectingOptionsLayout layoutLoansYear;

    private WritingOptionLayout layoutLoansValue;

    private SelectingOptionsLayout layoutLoansRates;

    private TextView btnApplyCal;

    public CalSingleLoansFragment() {
        super();
        setArguments(new Bundle());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            return;
        }
        LoanWay loanWay = (LoanWay) getArguments().getSerializable(WAY);
        presenter.chooseLoanWay(loanWay);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        presenter = new CalSingleLoanPresenter(
                this, new SingleLoanWayInputData(),
                (OnLoanWaySwitchListener) getActivity(),
                (CalculateResultListener) getActivity()
        );
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parentView = inflater.inflate(R.layout.fragment_cal_single_loans, container, false);
        initViews(parentView);
        return parentView;
    }

    private void initViews(final View parentView) {
        layoutLoansWay = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_loans_way);
        layoutLoansWay.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseLoanWay();
            }
        });

        layoutLoansRates = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_loans_rates);
        layoutLoansRates.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseLoanRates();
            }
        });

        layoutLoansValue = (WritingOptionLayout) parentView.findViewById(R.id.layout_loan_value);
        layoutLoansValue.setInputValidator(presenter.asInputValidator());
        layoutLoansValue.setInputStateListener(presenter.asInputStateListener());

        layoutLoansYear = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_loans_years);
        layoutLoansYear.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseLoanYear();
            }
        });

        btnApplyCal = (TextView) parentView.findViewById(R.id.btn_apply_cal);
        btnApplyCal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.applyCalculate();
            }
        });
    }

    @Override
    public void showLoanWays() {
        PopupWindowHelper.callPopWinWithSingleWv(
                parentView, 0,
                OptionsProvider.getLoansWayNames(),
                OptionsProvider.getMapLoansWay(),
                new WheelView.SingleWvCallBack<LoanWay>() {
                    @Override
                    public void onSelected(LoanWay loanWay) {
                        presenter.chooseLoanWay(loanWay);
                    }
                }
        );
    }

    @Override
    public void showLoanWay(String loansWayExpress) {
        layoutLoansWay.setRightTextViewText(loansWayExpress);
        layoutLoansWay.setRightTextViewFontColor(R.color.txt_option_main_color);
    }

    @Override
    public void showLoanYears() {
        PopupWindowHelper.callPopWinWithSingleWv(
                parentView, 0,
                OptionsProvider.getYearsStr(),
                OptionsProvider.getMapYear(),
                new WheelView.SingleWvCallBack<Integer>() {
                    @Override
                    public void onSelected(Integer year) {
                        presenter.chooseLoanYear(year);
                    }
                });
    }

    @Override
    public void showLoanYear(String yearExpress) {
        layoutLoansYear.setRightTextViewText(yearExpress);
        layoutLoansYear.setRightTextViewFontColor(R.color.txt_option_main_color);
    }

    @Override
    public void showLoanRates() {
        List<String> optionNamesLeft = OptionsProvider.getRateNames();
        List<String> optionNamesRight = OptionsProvider.getDiscountNames();

        HashMap<String, LoanRate> optionsLeft = OptionsProvider.getMapLoansRate();
        HashMap<String, Float> rightOptions = OptionsProvider.getMapDiscount();

        PopupWindowHelper.callPopWinWithTwoWv(
                parentView, 0, OptionsProvider.DISCOUNT_DEFAULT_POSITION,
                optionNamesLeft, optionNamesRight, optionsLeft, rightOptions,
                new WheelView.TwoWvCallBack<LoanRate, Float>() {
                    @Override
                    public void onSelected(LoanRate loanRate, Float disCount) {
                        presenter.chooseLoanRates(loanRate, disCount);
                    }
                });
    }

    @Override
    public void showLoanRate(String rateExpress) {
        layoutLoansRates.setRightTextViewText(rateExpress);
        layoutLoansRates.setRightTextViewFontColor(R.color.txt_option_main_color);
    }

    @Override
    public void tipError(String s) {
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void resetLoanRates() {
        layoutLoansRates.setRightTextViewText(R.string.please_choose_loan_rate);
        layoutLoansRates.setRightTextViewFontColor(R.color.txt_option_sub_text);
    }

    @Override
    public void resetLoanYears() {
        layoutLoansYear.setRightTextViewText(R.string.please_choose_loan_year);
        layoutLoansYear.setRightTextViewFontColor(R.color.txt_option_sub_text);
    }

    @Override
    public void showEnableButton() {
        btnApplyCal.setEnabled(true);
        btnApplyCal.setBackgroundResource(R.drawable.button_cal);
    }

    @Override
    public void showDisableButton() {
        btnApplyCal.setEnabled(false);
        btnApplyCal.setBackgroundResource(R.drawable.bg_common_btn_disabled);
    }
}